package com.fox.stock.security.util;

import com.fox.stock.mapper.SysUserMapper;
import com.fox.stock.pojo.domain.PermissionDomain;
import com.fox.stock.pojo.entity.SysPermission;
import com.fox.stock.pojo.vo.resq.LoginRespVo;

import java.util.ArrayList;
import java.util.List;

public class PermissionTreeUtil {

    public static List<PermissionDomain> getMenus(List<SysPermission> list){
        // 5. 递归用户的权限集合,组装处权限树(menus) 不包含按钮权限集合
        List<PermissionDomain> best = new ArrayList<>();
        getPermissionTree(list, best);

        // best集合就是目录集合(并且菜单已经链在目录下)
        return best;
    }

    public static List<String> getPermissions(List<SysPermission> list){
        // 7. 获取按钮权限标识集合
        List<String> perms = new ArrayList<>();
        for (SysPermission sysPermission : list) {
            if (sysPermission.getType() == 3) {
                perms.add(sysPermission.getCode());
            }
        }
        return perms;
    }

    private static void getPermissionTree(List<SysPermission> list, List<PermissionDomain> best) {
        for (SysPermission sysPermission : list) {
            if (sysPermission.getPid() == 0) {
                best.add(PermissionDomain.changeBySysPermission(sysPermission));
            }
        }
        for (SysPermission sysPermission : list) {
            if (sysPermission.getPid() != 0 && sysPermission.getType() == 2) {
                for (PermissionDomain permissionDomain : best) {
                    if (permissionDomain.getId() == sysPermission.getPid()) {
                        permissionDomain.getChildren().add(PermissionDomain.changeBySysPermission(sysPermission));
                    }
                }
            }
        }
//        for (SysPermission permission : list) {
//            if (permission.getPid() != 0 && permission.getType() == 3){
//                for (PermissionDomain permissionDomain : best) {
//                    List<PermissionDomain> children = permissionDomain.getChildren();
//                    for (PermissionDomain child : children) {
//                        if (child.getId() == permission.getPid()){
//                            child.getChildren().add(PermissionDomain.changeBySysPermission(permission));
//                        }
//                    }
//                }
//            }
//        }
    }

    private static void getPermissionTree1(List<SysPermission> list, List<PermissionDomain> best) {
        for (SysPermission sysPermission : list) {
            if (sysPermission.getPid() == 0) {
                best.add(PermissionDomain.changeBySysPermission(sysPermission));
            }
        }
        for (SysPermission sysPermission : list) {
            if (sysPermission.getPid() != 0 && sysPermission.getType() == 2) {
                for (PermissionDomain permissionDomain : best) {
                    if (permissionDomain.getId() == sysPermission.getPid()) {
                        permissionDomain.getChildren().add(PermissionDomain.changeBySysPermission(sysPermission));
                    }
                }
            }
        }
        for (SysPermission permission : list) {
            if (permission.getPid() != 0 && permission.getType() == 3) {
                for (PermissionDomain permissionDomain : best) {
                    List<PermissionDomain> children = permissionDomain.getChildren();
                    for (PermissionDomain child : children) {
                        if (child.getId() == permission.getPid()) {
                            child.getChildren().add(PermissionDomain.changeBySysPermission(permission));
                        }
                    }
                }
            }
        }
    }

//    // 递归实现含有按钮的权限树
//    private List<SysPermission> getPermissionTree1(List<SysPermission> list){
//        for (SysPermission sysPermission : list) {
//            if (sysPermission.getType() != 1){
//                sysPermission.setch(getPermissionTree1())
//            }
//        }
//    }

}
